<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Conexion
 *
 * @author carlos
 */
class Conexion //clase que utiliza las funcionas principales de postgres para utilizar las funciones principales
{
    private $host; //host de conexion
    private $dbName; //nombre de la base de datos
    private $user; //nombre del usuario
    private $password; //nombre del password
    private $puerto;
    private $dbconn;
            
    function __construct($host, $dbName, $user, $password,$puerto) {
        $this->host = $host;
        $this->dbName = $dbName;
        $this->user = $user;
        $this->password = $password;
        $this->puerto=$puerto;
    }

    public function conectar() //conecta a la base de datos
    {
                //echo $this->getStringConexion();
                $this->dbconn=  pg_connect($this->getStringConexion()) or die ("Error de conexion.". pg_last_error());;
    }
    
    public function desconectar() //desconecta de la base de datos
    {
                pg_close($this->dbconn);
    }
    
    public function getStringConexion() //devuelve el string de conexion
    {
        $strCnx = "host=$this->host port=$this->puerto  dbname=$this->dbName user=$this->user password=$this->password";
        return $strCnx;
    }
    
    public function ejecutarConsulta($query)
    {
        
        $this->conectar();
        $result = pg_query($query) or die('La consulta fallo: ' . pg_last_error());        
        //pg_query($query) or die('La consulta fallo: ' . pg_last_error());        
        $this->desconectar();
        return $result;
    }
            
}
